﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace zsShowHideRibbon
{
    public class ShowHideRibbon : Control
    {
        protected override void CreateChildControls()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("<link type=\"text/css\" rel=\"stylesheet\" media=\"all\"  href=\"/Style Library/css/style.css\" />");
            stringBuilder.AppendLine("<link type=\"text/css\" rel=\"stylesheet\" media=\"all\"  href=\"/Style Library/css/slide.css\" />");
            stringBuilder.AppendLine("<script type=\"text/javascript\" src=\"/Style Library/js/jquery-1.4.2.min.js\"></script>");
            stringBuilder.AppendLine("<script type=\"text/javascript\" src=\"/Style Library/js/jquery.cookie.js\"></script>");
            //stringBuilder.AppendLine("<script type=\"text/javascript\" src=\"/Style Library/js/slide.js\"></script>");
            stringBuilder.AppendLine("<script>");
            stringBuilder.AppendLine(" $(document).ready(function () {");
            stringBuilder.AppendLine("// Add Ribbon Tab");
            stringBuilder.AppendLine("var addRibbonTab = \"<div id='ribbon-tab' class='tab'><ul class='login'><li class='left'>&nbsp;</li><li class='login-content welcome-login'></li><li class='login-content sep'>|</li><li id='toggle' class='login-content'><a id='open' class='open'>Open Ribbon</a><a id='close' style='display: none;' class='close'>Close Ribbon</a></li><li class='right'>&nbsp;</li></ul> </div> \";");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Remove body attribute [scroll]");
            stringBuilder.AppendLine("$(\"body\").removeAttr(\"scroll\");");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Remove Ribbon Panel Attribute [id] to prevent fixed height");
            stringBuilder.AppendLine("$(\"#s4-titlerow\").removeAttr(\"id\");");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Remove Workspace panel attribute [id] to prevent panel scrolling");
            stringBuilder.AppendLine("$(\"#s4-workspace\").attr(\"id\", \"s4-workspace-autoscroll\");");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Remove class add custom class to view browse tab");
            stringBuilder.AppendLine("$(\".ms-cui-tts-scale-2\").removeClass().addClass(\"ms-cui-tts\");");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Add Ribbon Tab into Ribbon panel");
            stringBuilder.AppendLine("$(\"#s4-ribbonrow\").append(addRibbonTab);");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Ribbon Cookie Current state");
            stringBuilder.AppendLine("var ribbonCurrentState = $.cookie('ribbonCurrentState');");
            stringBuilder.AppendLine("var options = { path: '/', expires: 10 };");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Expand Panel");
            stringBuilder.AppendLine("$(\"#open\").click(function () {");
            stringBuilder.AppendLine("$(\"div#s4-ribboncont\").slideDown(\"slow\");");
            stringBuilder.AppendLine("$.cookie('ribbonCurrentState', 'expanded', options);");
            stringBuilder.AppendLine("});");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Collapse Panel");
            stringBuilder.AppendLine("$(\"#close\").click(function () {");
            stringBuilder.AppendLine("$(\"div#s4-ribboncont\").slideUp(\"slow\");");
            stringBuilder.AppendLine("$.cookie('ribbonCurrentState', 'collapsed', options);");
            stringBuilder.AppendLine("});");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Switch buttons from 'Open Ribbon' to 'Close Ribbon' on click");
            stringBuilder.AppendLine("$(\"#toggle a\").click(function () {");
            stringBuilder.AppendLine("$(\"#toggle a\").toggle();");
            stringBuilder.AppendLine("});");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("// Ribbon Cookie state");
            stringBuilder.AppendLine("if (ribbonCurrentState == null) {");
            stringBuilder.AppendLine("$.cookie('ribbonCurrentState', 'expanded', options);");
            stringBuilder.AppendLine("};");
            stringBuilder.AppendLine("if (ribbonCurrentState == 'collapsed') {");
            stringBuilder.AppendLine("$(\"div#s4-ribboncont\").css({ 'display': 'none' });");
            stringBuilder.AppendLine("$(\"#close\").css({ 'display': 'none' });");
            stringBuilder.AppendLine("$(\"#open\").css({ 'display': 'block' });");
            stringBuilder.AppendLine("};");
            stringBuilder.AppendLine("if (ribbonCurrentState == 'expanded') {");
            stringBuilder.AppendLine("$(\"div#s4-ribboncont\").css({ 'display': 'block' });");
            stringBuilder.AppendLine("$(\"#open\").css({ 'display': 'none' });");
            stringBuilder.AppendLine("$(\"#close\").css({ 'display': 'block' });");
            stringBuilder.AppendLine("};");
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("});");
            stringBuilder.AppendLine("</script>");
            this.Controls.Add(new LiteralControl(stringBuilder.ToString()));

            base.CreateChildControls();
        }
    }
}
